import React from 'react';
import {Provider} from 'react-redux';
import {Provider as RNProvider} from '@ant-design/react-native';
import AppContainer from './routes';
import store, {persistor} from './store';
import {PersistGate} from 'redux-persist/integration/react';
import {StatusBar, AppState} from 'react-native';
import NaviagtorService from '~/utils/navigator';
import {checkUnreadMsgCount, resetMsgTimer} from '~/utils/backrun';

AppState.addEventListener('change', () => {
    if (AppState.currentState === 'background') {
        resetMsgTimer();
    }
    if (AppState.currentState === 'active') {
        const getter = store.getState();
        if (getter.userinfo.userid > 0) {
            checkUnreadMsgCount();
        } else {
            resetMsgTimer();
        }
    }
});

export default () => {
    return (
        <Provider store={store}>
            <PersistGate persistor={persistor}>
                <StatusBar translucent backgroundColor="transparent" barStyle="dark-content" />
                <RNProvider>
                    <AppContainer
                        ref={(ref) => {
                            NaviagtorService.setNaviagtor(ref);
                        }}
                    />
                </RNProvider>
            </PersistGate>
        </Provider>
    );
};
